package com.moinut.studentattendancemanager.view.student;

import com.moinut.studentattendancemanager.UserManager;
import com.moinut.studentattendancemanager.controller.AttendanceController;
import com.moinut.studentattendancemanager.controller.callback.OnGetListListener;
import com.moinut.studentattendancemanager.model.vo.AttendanceInfo;
import com.moinut.studentattendancemanager.utils.WindowJumper;
import com.moinut.studentattendancemanager.view.BaseFrame;
import com.moinut.studentattendancemanager.view.comment.ChangePasswordFrame;
import com.moinut.studentattendancemanager.view.comment.MainLoginFrame;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

/**
 * @author MOILING
 */
public class StudentPageFrame extends BaseFrame<StudentPageFrame.StudentPagePanel> {
    private static final long serialVersionUID = 1L;

    private static final int DEFAULT_WIDTH = 650;
    private static final int DEFAULT_HEIGHT = 550;


    public StudentPageFrame() {
        super("学生页面", DEFAULT_WIDTH, DEFAULT_HEIGHT);
        getContentPanel().setStudentId(UserManager.userId);
    }

    @Override
    protected StudentPagePanel onSetContentPane() {
        return new StudentPagePanel();
    }

    /*package*/ class StudentPagePanel extends JPanel implements ActionListener {
        private static final long serialVersionUID = 1L;

        private JLabel mStudentPicLabel;
        private JLabel mStudentIdLabel, mSearchLabel;
        // option panel
        private JButton mGetInfoButton, mChangeInfoButton, mChangePasswordButton,
                mGetTeacherButton, mGetCourseButton, mExitButton;
        private JButton mSearchButton;
        private JTextField mSearchTextField;
        private JTable mResultTable;
        private JPanel mStudentInfoPanel, mOptionPanel, mSearchPanel, mResultPanel;
        private JScrollPane mResultScrollPane;
        private DefaultTableModel mTableModel;

        /*package*/ StudentPagePanel() {
            setLayout(null);
            initStudentInfoPanel();
            initOptionPanel();
            initSearchPanel();
            initResultPanel();
        }

        private void initResultPanel() {
            String[] columnName = {"考勤时间", "考勤课程", "课程时间", "考勤结果"};
            mTableModel = new DefaultTableModel(null, columnName);
            mResultTable = new JTable(mTableModel);
            mResultScrollPane = new JScrollPane(mResultTable,
                    ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

            mResultPanel = new JPanel();
            mResultPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            mResultPanel.setSize(475, 440);
            mResultPanel.setLocation(160, 80);
            mResultPanel.setBorder(new LineBorder(Color.GRAY));

            mResultPanel.add(mResultScrollPane);

            add(mResultPanel);
        }

        private void initSearchPanel() {
            mSearchLabel = new JLabel("请输入要查询考勤记录的课程编号");
            mSearchTextField = new JTextField(36);
            mSearchButton = new JButton("查询");

            mSearchPanel = new JPanel();
            mSearchPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            mSearchPanel.setSize(475, 70);
            mSearchPanel.setLocation(160, 5);
            mSearchPanel.setBorder(new LineBorder(Color.GRAY));

            mSearchPanel.add(mSearchLabel);
            mSearchPanel.add(mSearchTextField);
            mSearchPanel.add(mSearchButton);

            add(mSearchPanel);

            mSearchButton.addActionListener(this);
        }

        private void initOptionPanel() {
            mGetInfoButton = new JButton("查看个人信息");
            mChangeInfoButton = new JButton("修改个人信息");
            mChangePasswordButton = new JButton("修改个人密码");
            mGetTeacherButton = new JButton("查看教师信息");
            mGetCourseButton = new JButton("查看课程信息");
            mExitButton = new JButton("退出当前账号");

            mOptionPanel = new JPanel();
            mOptionPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
            mOptionPanel.setSize(150, 320);
            mOptionPanel.setLocation(5, 190);
            mOptionPanel.setBorder(new LineBorder(Color.GRAY));

            mOptionPanel.add(mGetInfoButton);
            mOptionPanel.add(mChangeInfoButton);
            mOptionPanel.add(mChangePasswordButton);
            mOptionPanel.add(mGetTeacherButton);
            mOptionPanel.add(mGetCourseButton);
            mOptionPanel.add(mExitButton);

            add(mOptionPanel);

            mGetInfoButton.addActionListener(this);
            mChangeInfoButton.addActionListener(this);
            mChangePasswordButton.addActionListener(this);
            mGetTeacherButton.addActionListener(this);
            mGetCourseButton.addActionListener(this);
            mExitButton.addActionListener(this);
        }

        private void initStudentInfoPanel() {
            mStudentPicLabel = new JLabel();
            mStudentPicLabel.setSize(140, 140);

            ImageIcon image = new ImageIcon(this.getClass().getResource("/res/images/portrait.jpg"));
            image.setImage(image.getImage().getScaledInstance(mStudentPicLabel.getWidth(),
                    mStudentPicLabel.getHeight(),
                    Image.SCALE_DEFAULT));
            mStudentPicLabel.setIcon(image);

            mStudentIdLabel = new JLabel();

            mStudentInfoPanel = new JPanel();
            mStudentInfoPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
            mStudentInfoPanel.setSize(150, 180);
            mStudentInfoPanel.setLocation(5, 5);
            mStudentInfoPanel.setBorder(new LineBorder(Color.GRAY));

            mStudentInfoPanel.add(mStudentPicLabel);
            mStudentInfoPanel.add(mStudentIdLabel);

            add(mStudentInfoPanel);
        }

        private void clearTable() {
            for (int i = mTableModel.getRowCount() - 1; i >= 0; i--) {
                mTableModel.removeRow(i);
            }
        }

        private void getAttendanceInfo() {
            AttendanceController.getAttendancesInfoByCourseIdAndStudentId(mSearchTextField.getText(), UserManager.userId, new OnGetListListener<AttendanceInfo>() {
                @Override
                public void onSuccess(List<AttendanceInfo> attendancesInfo) {
                    clearTable();
                    for (AttendanceInfo attendanceInfo : attendancesInfo) {
                        mTableModel.addRow(new Object[]{
                                attendanceInfo.getTime(),
                                attendanceInfo.getCourse(),
                                attendanceInfo.getClassTime(),
                                attendanceInfo.getState()
                        });
                    }
                }

                @Override
                public void onError(String error) {
                    clearTable();
                    JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
                }
            });
        }

        /*package*/ void setStudentId(String studentId) {
            mStudentIdLabel.setText(studentId);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            clearTable();
            if (e.getSource().equals(mGetInfoButton)) {
                WindowJumper.jump(StudentPageFrame.this, StudentInfoFrame.class);
            } else if (e.getSource().equals(mChangePasswordButton)) {
                WindowJumper.jump(StudentPageFrame.this, ChangePasswordFrame.class);
            } else if (e.getSource().equals(mChangeInfoButton)) {
                WindowJumper.jump(StudentPageFrame.this, ChangeStudentInfoFrame.class);
            } else if (e.getSource().equals(mGetTeacherButton)) {
                WindowJumper.jump(StudentPageFrame.this, GetTeacherFrame.class);
            } else if (e.getSource().equals(mGetCourseButton)) {
                WindowJumper.jump(StudentPageFrame.this, GetCourseFrame.class);
            } else if (e.getSource().equals(mSearchButton)) {
                getAttendanceInfo();
            } else if (e.getSource().equals(mExitButton)) {
                UserManager.exit();
                WindowJumper.jump(StudentPageFrame.this, MainLoginFrame.class);
            }
        }
    }
}
